package com.tanhua.recommend.listener;

import com.tanhua.domain.mongo.VideoScore;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(topic = "video-recommend", consumerGroup = "video-recommend-consumer")
public class VideoScoreListener implements RocketMQListener<Map> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(Map map) {
        System.out.println("接收小视频消息：" + map);

        // 1、取出数据
        Long userId = Long.valueOf((String) map.get("userId"));
        Long vid = Long.valueOf((String) map.get("vid"));
        Integer type = Integer.valueOf((String) map.get("type"));
        // 2、封装实体
        VideoScore videoScore = new VideoScore();
        videoScore.setUserId(userId);
        videoScore.setVideoId(vid);
        videoScore.setDate(System.currentTimeMillis());

        // 计算分值
        switch (type) {
            case 1: {
                videoScore.setScore(20d);
                break;
            }
            case 2: {
                videoScore.setScore(5d);
                break;
            }
            case 3: {
                videoScore.setScore(-5d);
                break;
            }
            case 4: {
                videoScore.setScore(10d);
                break;
            }
        }

        // 3、保存实体
        mongoTemplate.save(videoScore);

    }
}
